//
//  ViewController.swift
//  HAHA
//
//  Created by Bailinhan on 2017/9/26.
//  Copyright © 2017年 Bailinhan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var ling: UIButton!
    @IBOutlet weak var yi: UIButton!
    @IBOutlet weak var er: UIButton!
    @IBOutlet weak var san: UIButton!
    @IBOutlet weak var si: UIButton!
    @IBOutlet weak var wu: UIButton!
    @IBOutlet weak var liu: UIButton!
    @IBOutlet weak var qi: UIButton!
    @IBOutlet weak var ba: UIButton!
    @IBOutlet weak var jiu: UIButton!
    @IBOutlet weak var jia: UIButton!
    @IBOutlet weak var jian: UIButton!
    @IBOutlet weak var cheng: UIButton!
    @IBOutlet weak var chu: UIButton!
    @IBOutlet weak var calculate: UIButton!
    @IBOutlet weak var result: UILabel!
    @IBOutlet weak var quyu: UIButton!
    public var inputA:Double = 0
    public var inputB:Double = 0
    public var current:Double = 0
    public var state:Int = 0
    public var type:Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        result.text = "\(current)"
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    @IBAction func numberAdd(_ sender: UIButton) {
        let item:String = sender.currentTitle!

        switch item {
        case "0":current = current * 10
                result.text = "\(current)"
                break
        case "1":current = current * 10 + 1
            result.text = "\(current)"
            break
        case "2":current = current * 10 + 2
            result.text = "\(current)"
            break
        case "3":current = current * 10 + 3
            result.text = "\(current)"
            break
        case "4":current = current * 10 + 4
            result.text = "\(current)"
            break
        case "5":current = current * 10 + 5
            result.text = "\(current)"
            break
        case "6":current = current * 10 + 6
            result.text = "\(current)"
            break
        case "7":current = current * 10 + 7
            result.text = "\(current)"
            break
        case "8":current = current * 10 + 8
            result.text = "\(current)"
            break
        case "9":current = current * 10 + 9
            result.text = "\(current)"
            break
        case "000":current = current * 1000
        result.text = "\(current)"
            break
        case "%":current = current / 100
            result.text = "\(current)"
        case "清零":state  = 0
        inputA = 0
        current = 0
        type = 0
        result.text = "归零成功"
        break
        default:
            return
        }
    }
    
    func howto(){
        state = 1
        if(inputA != 0){
        }
        else{
        inputA = current
        }
        current = 0
    }
    
    
    @IBAction func howTo(_ sender: UIButton) {
        if(state == 1){
            return
        }
        let item:String = sender.currentTitle!
        switch item {
        case "+":result.text = "➕"
            type = 1
            howto()
            break
        case "-":result.text = "➖"
        type = 2
        howto()
            break
            
        case "x":result.text = "✖️"
        type = 3
        howto()
            break
            
        case "/":result.text = "➗"
        type = 4
            howto()
            break
        default:
            return
        }
    }

    
    @IBAction func calculate(_ sender: UIButton) {
        if(state == 0){
            return
        }
        switch type {
        case 1:inputA = inputA + current
            break
        case 2:inputA = inputA - current
            break
        case 3:inputA = inputA * current
            break
        case 4:inputA = inputA / current
            break
        case 6:inputA = inputA / 100
            break
        default:
            return
        }
        state = 0
        result.text = "\(inputA)"
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

